package com.stn.zjx.givingtreasure.activity.bi;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.came.viewbguilib.ButtonBgUi;
import com.google.gson.Gson;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.progresslayout.ProgressLayout;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.adapter.TokenSearchItemListAdapter;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.domain.TokenListDomain;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.net.BaseApi;
import com.stn.zjx.givingtreasure.net.impl.EthNetImpl;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddAssetsActivity extends BaseActivity {
    @BindView(R.id.back_bt)
    RelativeLayout backBt;
    @BindView(R.id.action_title)
    TextView actionTitle;
    @BindView(R.id.web_title)
    TextView webTitle;
    @BindView(R.id.action_add_bt)
    RelativeLayout actionAddBt;
    @BindView(R.id.action_refresh)
    RelativeLayout actionRefresh;
    @BindView(R.id.exit)
    RelativeLayout exit;
    @BindView(R.id.sure_bt)
    TextView sureBt;
    @BindView(R.id.skip_bt)
    TextView skipBt;
    @BindView(R.id.share_bt)
    RelativeLayout shareBt;
    @BindView(R.id.more_bt)
    RelativeLayout moreBt;
    @BindView(R.id.release_bt)
    ButtonBgUi releaseBt;
    @BindView(R.id.search_iv)
    ImageView searchIv;
    @BindView(R.id.search_edit)
    EditText searchEdit;
    @BindView(R.id.search_bg)
    RelativeLayout searchBg;
    @BindView(R.id.b_rv)
    RecyclerView bRv;
    @BindView(R.id.twinkling_refresh)
    TwinklingRefreshLayout twinklingRefresh;
    TokenSearchItemListAdapter adapter;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    private LinearLayoutManager lm;
    public String address = "";

    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }

    @Override
    public void initComponent() {
        searchBg.setVisibility(View.VISIBLE);
        address = getIntent().getStringExtra("address");
        lm = new LinearLayoutManager(this, OrientationHelper.VERTICAL, false);
        bRv.setLayoutManager(lm);
        bRv.setAdapter(adapter = new TokenSearchItemListAdapter(this));

        ProgressLayout headerView = new ProgressLayout(this);
        headerView.setColorSchemeResources(R.color.bg_red_orange, R.color.bg_red_orange1);
        twinklingRefresh.setHeaderView(headerView);
        twinklingRefresh.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
                initRefresh(searchEdit.getText().toString().trim());
            }
        });
        twinklingRefresh.startRefresh();
        searchEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                initRefresh(searchEdit.getText().toString().trim());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_assets);
        ButterKnife.bind(this);
        initComponent();
    }

    @OnClick({R.id.back_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_bt:
                finish();
                break;
        }
    }

    public void initRefresh(String like) {
        EthNetImpl
                .eth_token_show(like, address, address + "_eth_token_show", new StringCallback() {
                    @Override
                    public void onFinish() {
                        super.onFinish();
                        twinklingRefresh.finishRefreshing();
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                          if(response.isSuccessful()&&!isFinishing()){
                              TokenListDomain domain = new Gson().fromJson(response.body(), TokenListDomain.class);
                              switch (domain.getStatus()) {
                                  case BaseApi.REQUEST_SUCCESS_CODE:
                                      if (domain.getData() != null && domain.getData().size() != 0) {
                                          noDataTv.setVisibility(View.GONE);
                                          adapter.refreshList(domain.getData());
                                      } else {
                                          adapter.clearList();
                                          noDataTv.setVisibility(View.VISIBLE);
                                      }
                                      break;
                                  default:
                                      adapter.clearList();
                                      noDataTv.setVisibility(View.VISIBLE);
                                      break;
                              }
                          }

                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        if(response.isSuccessful()&&!isFinishing()){
                            TokenListDomain domain = new Gson().fromJson(response.body(), TokenListDomain.class);
                            switch (domain.getStatus()) {
                                case BaseApi.REQUEST_SUCCESS_CODE:
                                    if (domain.getData() != null && domain.getData().size() != 0) {
                                        noDataTv.setVisibility(View.GONE);
                                        adapter.refreshList(domain.getData());
                                    } else {
                                        adapter.clearList();
                                        noDataTv.setVisibility(View.VISIBLE);
                                    }
                                    break;
                                default:
                                    adapter.clearList();
                                    noDataTv.setVisibility(View.VISIBLE);
                                    break;
                            }
                        }
                    }
                });
    }



}
